# .........................................................................
# Title: nonus_estimates.py
#
# Implements security-level regression on the sample of non-US corporate
# bonds, and displays the results alongside those obtained on the US
# sample when using an analogous proxy for safe ownership
# .........................................................................

import pandas as pd
import numpy as np
import econtools.metrics as mt
from econtools.util.frametools import group_id

# directories
PROJECT_PATH = "<PROJECT_PATH>/"
TMP = PROJECT_PATH + "temp/"
TABLES = PROJECT_PATH + "tables/"

# read in proxy variable us regression
us_proxy_results = pd.read_stata(TMP + "us_proxy_regression_results.dta")
us_proxy_results = us_proxy_results.drop(columns=['level_0']).T
us_proxy_results.columns = ['USA_Proxy']

# read in data
reg_data = pd.read_stata(TMP + "nonus_regressions_panel.dta")
trace_catalog = pd.read_stata(TMP + "trace_clean_catalog.dta")
reg_data['log_hedged_drawdown'] = np.log(reg_data['hedged_drawdown'])
assert (trace_catalog.in_trace_universe == 1).all()

# specification
outcome = 'log_hedged_drawdown'
regressor = 'nonfund_share'
cluster_var = 'firm_event_cluster'
weights = 'value_outstanding'
currency_dummies = ["currency_USD", "currency_EUR", "currency_GBP", "currency_CAD", "currency_CHF", "currency_JPY"]

# ensure no overlap with trace
reg_data = reg_data[~reg_data.cusip.isin(trace_catalog.cusip)]

# regressions
working_regs = {}
x_list = [regressor] + currency_dummies
for country in ['EMU', 'CAN', 'GBR']:
    working_regs[country] = mt.reg(reg_data[reg_data.country_bg == country], outcome, x_list, fe_name='full_fe_category', awt_name=weights, cluster=cluster_var)
working_regs['ALL'] = mt.reg(reg_data, outcome, x_list, fe_name='full_fe_category', awt_name=weights, cluster=cluster_var)

# number of identifying issuers
def get_identifying_issuers(country, df, regs_dict):
    if country != 'ALL':
        df = df[df.country_bg == country]
    return len(set(df[regs_dict[country].sample]['consolidated_issuer_id']))
n_id_issuers = {x: get_identifying_issuers(x, df=reg_data, regs_dict=working_regs) for x in ['ALL', 'EMU', 'CAN', 'GBR']}

# gather results
working_results = pd.DataFrame([
    {k: round(v.beta.values[0], 2) for k,v in working_regs.items()},
    {k: round(v.se.values[0], 2) for k,v in working_regs.items()},
    {k: round(v.r2, 2) for k,v in working_regs.items()},
    {k: int(v.N) for k,v in working_regs.items()},
    {k: int(n_id_issuers[k]) for k,v in working_regs.items()}
], index=['beta', 'se', 'r2', 'N', 'n_id_firms'])

# add us proxy results
working_results = working_results.join(us_proxy_results)

# construct and output non-US panel for display (Table 5)
dummy_labels = {
    'Event Dummy': {'EMU': 'Yes', 'CAN': 'Yes', 'GBR': 'Yes', 'ALL': 'Yes', 'USA_Proxy': 'Yes'},
    'Bond Size' : {'EMU': 'Yes', 'CAN': 'Yes', 'GBR': 'Yes', 'ALL': 'Yes', 'USA_Proxy': 'Yes'},
    'Bond Duration' : {'EMU': 'Yes', 'CAN': 'Yes', 'GBR': 'Yes', 'ALL': 'Yes', 'USA_Proxy': 'Yes'},
    'Bond Rating' : {'EMU': 'Yes', 'CAN': 'Yes', 'GBR': 'Yes', 'ALL': 'Yes', 'USA_Proxy': 'Yes'},
    'Issuer Dummy' : {'EMU': 'Yes', 'CAN': 'Yes', 'GBR': 'Yes', 'ALL': 'Yes', 'USA_Proxy': 'Yes'},
    'Callable Dummy' : {'EMU': 'Yes', 'CAN': 'Yes', 'GBR': 'Yes', 'ALL': 'Yes', 'USA_Proxy': 'Yes'},
    'Senior Dummy' : {'EMU': 'Yes', 'CAN': 'Yes', 'GBR': 'Yes', 'ALL': 'Yes', 'USA_Proxy': 'Yes'},
    'Coupon Type' : {'EMU': 'Yes', 'CAN': 'Yes', 'GBR': 'Yes', 'ALL': 'Yes', 'USA_Proxy': 'Yes'},
    'Currency FE' : {'EMU': 'Yes', 'CAN': 'Yes', 'GBR': 'Yes', 'ALL': 'Yes', 'USA_Proxy': '--'},
    'Sample' : {'EMU': 'EMU', 'CAN': 'CAN', 'GBR': 'GBR', 'ALL': 'Pooled Non-US', 'USA_Proxy': 'USA'},
}
nonus_panel = working_results.T
for dummy in dummy_labels.keys():
    nonus_panel[dummy] = dummy_labels[dummy]
cols_display_order = ['USA_Proxy', 'ALL', 'EMU', 'GBR', 'CAN']
nonus_panel = nonus_panel.reindex(cols_display_order, axis=0)
nonus_panel = nonus_panel.rename(columns={'beta': 'Proxy Safe Share', 'se': 'Standard Error', 'N': 'Identifying Observations', 'n_id_firms': 'Identifying Issuers', 'r2': 'R2'})
row_display_order = ['Proxy Safe Share', 'Standard Error', 'Sample', 'Event Dummy', 'Bond Size', 'Bond Duration', 'Bond Rating', 'Issuer Dummy', 'Callable Dummy', 'Senior Dummy', 'Coupon Type', 'Currency FE', 'Identifying Observations', 'Identifying Issuers', 'R2']
nonus_panel = nonus_panel.reindex(row_display_order, axis=1).T
nonus_panel.columns = ['(1)', '(2)', '(3)', '(4)', '(5)']
nonus_panel.to_string(TABLES + "/nonus_panel.txt")
